<template>
<el-select v-model="val" :clearable="clearable" :placeholder="placeholder">
    <el-option v-for="item in dicts"
      :key="item.tmbh"
      :label="item.tmmc"
      :value="item.tmbh">
    </el-option>
  </el-select>
</template>
<script>
export default {
    name:'RsDict',
    props:{
        value:[String, Number],
        dict:String,
        clearable:{
            type:Boolean,
            default:false
        },
        placeholder:{
            type:String,
            default:'请选择...'
        }
    },
    data(){
        return {
            dicts:[],
            val:null,
            text:''
        }
    },
    created(){
        this.loadDicts();
        this.val = this.value === undefined? '':this.value + '';
    },
    watch:{
        val(newValue,oldValue){

            //console.log(typeof this.value)
            if( newValue != this.value ){
                this.$emit('input',newValue);
            }
        },
        value(newValue,oldValue){

            //console.log(typeof this.value)
            if( newValue != this.val ){
                this.val = newValue + '';
            }
        }
    },
    mounted(){
        
    },
    destroyed(){
        
    },
    methods:{
        loadDicts(){
            if(!this.dict) return;
            this.$req({
                url: "/comm/dict",
                method: 'post',
                data:{zdbh:this.dict}
            })
            .then(response => {
                const { data } = response;
                if( data.error_code == 0 ){
                    
                    this.dicts = data.dicts;
                }else{
                }
            }).catch(error => {
            });
        }
    }
}
</script>